package com.crm.finance.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crm.model.FinanceSchoolExpend;
import com.crm.model.SysDepartment;
import com.crm.model.SysUser;
import com.crm.service.FinanceSchoolExpendService;
import com.crm.service.SysDepartmentService;
import com.crm.util.ResponseUtil;
import com.crm.util.SessionModel;
import com.crm.util.SessionUtils;
import com.github.pagehelper.Page;

@Controller
@RequestMapping("/financeSchoolExpend")
public class FinanceSchoolExpendController {
	
	@Autowired
	private  FinanceSchoolExpendService financeSchoolExpendService;
	@Autowired
	private SysDepartmentService sysDepartmentService;
	
	@RequestMapping("/index")
	public String index(HttpServletRequest request){
		SysUser sysUser= ((SysUser)SessionUtils.get(SessionModel.SESSION_USER));
		request.setAttribute("user", sysUser);
		SysDepartment dept =selectDepartementByUser();
		if(Integer.valueOf(3).equals(dept.getType())){
			request.setAttribute("dept", dept);
		}else{
			request.setAttribute("dept", new SysDepartment());
		}
		return "financeCenter/schoolExpend";
	}
	
	@RequestMapping("/getAll")
	@ResponseBody
	public void getAll(HttpServletResponse response,HttpServletRequest request, Page<FinanceSchoolExpend> page, FinanceSchoolExpend t){
		
		Page<FinanceSchoolExpend> list = (Page<FinanceSchoolExpend>) financeSchoolExpendService.selectAll(page, t);
		
		ResponseUtil.resultPage(response,request,list);
	}
	
	@RequestMapping("/addNewRecord")
	@ResponseBody
	public Map<String, Object> addNewRecord(FinanceSchoolExpend record){
		
		Map<String, Object> map = new HashMap<>();
		
		financeSchoolExpendService.insert(record);
		
		map.put("status", "success");
		map.put("msg", "新增成功！");
		
		return map;
	}
	
	@RequestMapping("/updateRecord")
	@ResponseBody
	public Map<String, Object> updateRecord(FinanceSchoolExpend record){
		
		Map<String, Object> map = new HashMap<>();
		
		financeSchoolExpendService.updateByPrimaryKey(record);
		
		map.put("status", "success");
		map.put("msg", "修改成功！");
		
		return map;
	}
	
	@RequestMapping("/getApplyTimes")
	@ResponseBody
	public Map<String,Object> getApplyTimes(){
		Map<String,Object> retMap = new HashMap<>();
		String userId= ((SysUser)SessionUtils.get(SessionModel.SESSION_USER)).getUserId();
		int count=financeSchoolExpendService.getApplyTimesByUserId(userId);
		retMap.put("count", count);
		return retMap;
	}
	
	@RequestMapping("/selectAllByWhere")
	@ResponseBody
	public void selectAllByWhere(HttpServletResponse response,HttpServletRequest request,Page<FinanceSchoolExpend> page,FinanceSchoolExpend t){
		Page<FinanceSchoolExpend> list = (Page<FinanceSchoolExpend>) financeSchoolExpendService.selectAll(page, t);
		ResponseUtil.resultPage(response,request,list);
	}
	@RequestMapping("/getTotalMoneyByWhere")
	@ResponseBody
	public Map<String,Object> getTotalMoneyByWhere(FinanceSchoolExpend t){
		Map<String,Object> retMap = new HashMap<>();
		int totalMoney = financeSchoolExpendService.getTotalMoneyByWhere(t);
		retMap.put("data", totalMoney);
		return retMap;
	}
	private SysDepartment selectDepartementByUser(){
		SysUser sysUser =  (SysUser)SessionUtils.get(SessionModel.SESSION_USER);
		SysDepartment department = sysDepartmentService.selectByPrimaryKey(sysUser.getDepartmentId());
		if(department==null) return new SysDepartment();
		String fullPath = department.getFullPath();
		if(fullPath==null) return new SysDepartment();
		if("0".equals(fullPath)){
			return department;
		}else{
			department =sysDepartmentService.selectByPrimaryKey(fullPath.split(",")[1]);
			return department;
		}
		
	}
}
